<!--
 * @Author: 邓春宜
 * @Date: 2022-01-13 11:00:58
 * @LastEditTime: 2022-01-19 14:42:56
 * @LastEditors: 邓春宜
 * @Description: 
 * @FilePath: \TJS-EMS\web-ems\src\view\admin\sms\modal\add.vue
-->
<template>
  <a-modal
    :visible="visible"
    :title="t('add')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel()"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
     <a-form-item ref="receiver" :label="t('receiver')" name="receiver"><a-input v-model:value="formState.receiver" /> </a-form-item>
<a-form-item ref="field" :label="t('field')" name="field"><a-input v-model:value="formState.field" /> </a-form-item>
<a-form-item ref="content" :label="t('content')" name="content"><a-input v-model:value="formState.content" /> </a-form-item>
<a-form-item ref="status" :label="t('status')" name="status"><a-input-number v-model:value="formState.status" /> </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "ant-design-vue";
import { useI18n  } from "vue-i18n";
import { add, list } from "@/core/api/module/AdminSms";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [], 
      showPath: true, 
    });

    const formRef = ref();

    const formState = reactive({ 
    });
  
    const formRules = {
       receiver: [{ required: true, message:t('receiver') + t("must input"), trigger: "blur" }],
field: [{ required: true, message:t('field') + t("must input"), trigger: "blur" }],
content: [{ required: true, message:t('content') + t("must input"), trigger: "blur" }],
status: [{ required: true, message:t('status') + t("must input"), trigger: "blur",type:"number" }],
    };

    const submit = (e) => {
     
      formRef.value
        .validate()
        .then(() => {
         
          add(toRaw(formState)).then((response) => { 

            if (response.code == 0) {
              message.success({ content: t('Saved successfully!'), duration: 1 }).then(() => {
                cancel(true);
              });
            } else {
              message.error({ content: response.msg, duration: 1 }).then(() => {
                cancel(true);
              });
            }
          });
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };
   
    return {
      t,
      state,
      submit, 
      cancel,
      formRef,
      formState,
      formRules, 
      labelCol: { span: 6 },
      wrapperCol: { span: 18 }, 
    };
  },
});
</script>